import axios from 'axios'
import { Toast } from 'antd-mobile'
import errorCode from './../const/errorCode'
const Axios = axios.create({
  withCredentials: true,
  timeout: 10000
})
Axios.interceptors.request.use(
  config => {
    return config
  },
  error => {
    return Promise.reject(error)
  }
)
Axios.interceptors.response.use(
  data => {
    const errCode = data.data.code
    if (parseInt(errCode) === 1) {
      Toast.fail(data.data.msg)
    }
    return data
  },
  error => {
    const errMsg = error.toString()
    const code = errMsg.substr(errMsg.indexOf('code') + 5)
    Toast.fail(errorCode[code] || errorCode['default'])
    return Promise.reject(new Error(error))
  }
)
export default Axios
